package qq.listener;

import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

@Beans
public class QuestionListener {
    private static CloseableHttpClient httpclient = HttpClients.createDefault();
    @OnGroup
    public void question(GroupMsg groupMsg, MsgSender sender) throws IOException {
        String message = groupMsg.getMsg();
        if(message.startsWith("问 ")){
            String q = message.substring(2);
            RequestBuilder requestBuilder = RequestBuilder.get("https://so.csdn.net/api/v3/search?q=" + q);
            CloseableHttpResponse response = httpclient.execute(requestBuilder.build());
            HttpEntity httpEntity = response.getEntity();
            String s = EntityUtils.toString(httpEntity, "UTF-8");
            int i = s.indexOf("url_location\":\"");
            String substring = s.substring(i+15, i + 85);
            sender.SENDER.sendGroupMsg(groupMsg,substring);
        }


    }
}
